package com.szrt.javaweb;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloServlet implements Servlet {
	
	
	public HelloServlet(){
		System.out.println("执行构造方法...");
	}
	
	

	@SuppressWarnings("unchecked")
	@Override
	public void init(ServletConfig config) throws ServletException {
		
		System.out.println("init...");
		
		String user = config.getInitParameter("user");
		String password = config.getInitParameter("password");
		System.out.println("user="+user);
		System.out.println("password="+password);
		
		
		Enumeration<String> keys =config.getInitParameterNames();
		while(keys.hasMoreElements()){
			String key   =  keys.nextElement();
			String value =  config.getInitParameter(key);
			System.out.print("key:"+key +" value:"+value);
			System.out.println();
		}
		
		System.out.println(config.getServletName());
		
		
		//获取ServletContext对象 重点  CTRL+1
		ServletContext context = config.getServletContext();
		
		//1 获取当前WEB应用的初始化参数
		String charset =context.getInitParameter("charset");
		System.out.println(charset);
		keys = context.getInitParameterNames();
		while(keys.hasMoreElements()){
			String key   =  keys.nextElement();
			String value =  context.getInitParameter(key);
			System.out.print("key:"+key +" value:"+value);
			System.out.println();
		}
		
		String realPath = context.getRealPath("/img");
		System.out.println("realPath="+realPath);
		
		//返回斜杠加web应用的名称
		String contextPath = context.getContextPath();
		System.out.println("contextPath="+contextPath);
		
		
		//获取jdbc.properties文件中的内容 方法1
		InputStream in=null;
		try {
			ClassLoader classLoader = getClass().getClassLoader();
			in = classLoader.getResourceAsStream("jdbc.properties");
			
			System.out.println("ClassLoader读取文件"+in);
		} catch (Exception e) {
			System.out.println("路径错误...");
			e.printStackTrace();
		}finally{
			if(null !=in){
				try {
					System.out.println("关闭ClassLoader打开的流...");
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		//获取jdbc.properties文件中的内容 方法2
		InputStream in02=null;
		try {
			//  此处的正斜杠表示WEB应用的根目录
			//in02 = context.getResourceAsStream("/jdbc.properties");
			in02 = context.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
			System.out.println("ServletContext读取文件"+in02);
		} catch (Exception e) {
			System.out.println("路径错误...");
			e.printStackTrace();
		}finally{
			if(null !=in02){
				try {
					System.out.println("关闭ServletContext打开的流...");
					in02.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		
		
		
		
		
	}

	@Override
	public ServletConfig getServletConfig() {
		System.out.println("getServletConfig...");
		return null;
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) 
			throws ServletException, IOException {
		
		System.out.println("service...");
		
		
	}

	@Override
	public String getServletInfo() {
		System.out.println("getServletInfo...");
		return null;
	}

	@Override
	public void destroy() {
		System.out.println("destroy...");
		
	}

}
